package dice.sinanya.db.diceLog;

import com.forte.qqrobot.log.LogLevel;
import dice.sinanya.db.tools.SqliteUtil;
import dice.sinanya.entity.EntityLogList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class InsertDiceLog {
    private static final Logger logger = LogManager.getLogger(InsertDiceLog.class.getName());
    SqliteUtil sqliteUtil;
    Connection connection;
    int len=80;

    public InsertDiceLog() {
        sqliteUtil = new SqliteUtil();
        initCon();
        initDb();
    }

    private void initCon() {
        do {
            connection = sqliteUtil.getDiceLogConnection();
        } while (connection == null);
    }

    private void initDb() {
        try (Statement statement = connection.createStatement()) {
            statement.executeUpdate("create table if not exists diceLog(time datetime,level varchar(200),info longtext,PRIMARY KEY (time));");
        } catch (SQLException sqlException) {
            logger.error(sqlException.getMessage(), sqlException);
        }
    }

    public void insert(String msg, LogLevel logLevel) {
        try {
            insert(logLevel, msg);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }

    private void insert(LogLevel level, String info) throws SQLException {
        //language=SqLite
        if (connection == null || connection.isClosed()) {
            initCon();
        }
        String sql = "insert into diceLog(time,level,info) values(?,?,?);";
        try (PreparedStatement statement = connection.prepareStatement(sql)) {
            if (info.length()>len){
                for (String infoEvery:getStrList(info,len)){
                    statement.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
                    statement.setString(2, level.name());
                    statement.setString(3, infoEvery);
                    statement.executeUpdate();
                }
            }else{
                statement.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
                statement.setString(2, level.name());
                statement.setString(3, info);
                statement.executeUpdate();
            }

        } catch (SQLException ignore) {
        }
    }

    public static List<String> getStrList(String inputString, int length) {
        int size = inputString.length() / length;
        if (inputString.length() % length != 0) {
            size += 1;
        }
        return getStrList(inputString, length, size);
    }

    public static List<String> getStrList(String inputString, int length,
                                          int size) {
        List<String> list = new ArrayList<String>();
        for (int index = 0; index < size; index++) {
            String childStr = substring(inputString, index * length,
                    (index + 1) * length);
            list.add(childStr);
        }
        return list;
    }

    public static String substring(String str, int f, int t) {
        if (f > str.length())
            return null;
        if (t > str.length()) {
            return str.substring(f, str.length());
        } else {
            return str.substring(f, t);
        }
    }
}
